//
//  MaintableDetailController.m
//  DesighBook
//
//  Created by 梅明 on 15/9/22.
//  Copyright (c) 2015年 梅明. All rights reserved.
//

#import "MaintableDetailController.h"
#import "HttpRequestManager.h"
#import "DesighModel.h"
#import "UIImageView+AFNetworking.h"
#import "MainviewDetailCell.h"
#import "Helper.h"
#import "SecondScrollDetailViewController.h"

#define ImageHight 200.0f
#define viewwidth self.view.frame.size.width

@interface MaintableDetailController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_dataArray;
    UIImageView *_zoomImageView;
    UILabel *_label;
    UILabel *_label1;
    
}
@property (weak, nonatomic) IBOutlet UITableView *mytableView;
@end

@implementation MaintableDetailController
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return self;
}
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getDataFromNet];
    

}
-(void)viewWillAppear:(BOOL)animated
{
    _mytableView.delegate = self;
    _mytableView.dataSource = self;
}

-(void)viewWillDisappear:(BOOL)animated
{
    _mytableView.delegate = nil;
    _mytableView.dataSource = nil;
}


-(void)createZoompic
{
    
    //设置contentInset属性（上左下右 的值）
    _mytableView.contentInset = UIEdgeInsetsMake(ImageHight, 0, 0, 0);
    _mytableView.contentOffset = CGPointMake(0, -ImageHight);
    
    _mytableView.tableFooterView = [[UIView alloc]init];
    
    _zoomImageView = [[UIImageView alloc]init];
    _zoomImageView.image = nil;
    _zoomImageView.clipsToBounds = YES;
    [_zoomImageView setImageWithURL:[NSURL URLWithString:_imgurl]];
    _zoomImageView.frame = CGRectMake(0, -ImageHight, viewwidth, ImageHight);
    
    
    _zoomImageView.contentMode = UIViewContentModeScaleAspectFill;//重点（不设置那将只会被纵向拉伸）
    
    
    //将_zoomImageView添加到tableview上
    
    //用这一句,头像就不可以放在前面了
//    [_mytableView addSubview:_zoomImageView];
    
    //实现这一句头像就可以放在前面了
    [_mytableView insertSubview:_zoomImageView atIndex:0];
    
    //设置autoresizesSubviews让子类自动布局
    _zoomImageView.autoresizesSubviews = YES;
    
    UIImageView *backimg = [[UIImageView alloc]initWithFrame:CGRectMake(0, ImageHight-160, viewwidth, 160)];
    backimg.image = [UIImage imageNamed:@"mask_case_jiexi.png"];
    backimg.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [_zoomImageView addSubview:backimg];
    
    CGFloat height = [Helper heightOfString:_name font:[UIFont systemFontOfSize:20] width:200];
    _label = [[UILabel alloc]initWithFrame:CGRectMake(10, ImageHight-100, 200, height)];
    _label.text = _name;
    _label.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:20];
    _label.clipsToBounds = YES;
    _label.textColor = [UIColor whiteColor];
    _label.numberOfLines = 0;
    _label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;//自动布局，自适应顶部
//
    [_zoomImageView addSubview:_label];
    
    _label1 = [[UILabel alloc]initWithFrame:CGRectMake(10, ImageHight-100+height, 200, 30)];
    _label1.text = [NSString stringWithFormat:@"%@  %@  %@",_areaName,_spaceName,_styleName];
    _label1.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    _label1.clipsToBounds = YES;
    [_label1 setFont:[UIFont systemFontOfSize:13]];
    _label1.textColor = [UIColor whiteColor];
    [_zoomImageView addSubview:_label1];
    
   
    
}
-(void)getDataFromNet
{
    [[HttpRequestManager shareInstnce]getmaintableInfoWithID:_numberid AndSuccess:^(id responseObjec) {
        NSDictionary *dic = responseObjec;
        _areaName  = dic[@"areaName"];
        _comment = dic[@"comment"];
        _facepic = dic[@"facePic"];
        _feerand = dic[@"feeRand"];
        _imgurl = dic[@"imgUrl"];
        _name = dic[@"name"];
        _nick = dic[@"nick"];
        _phoneNumber = dic[@"phoneNum400"];
        _shareUrl = dic[@"shareUrl"];
        _spaceName = dic[@"spaceName"];
        _styleName = dic[@"styleName"];
       
        
        NSArray *arr = dic[@"imgList"];
        for (NSDictionary *dic1  in arr) {
            DesighModel *model = [[DesighModel alloc]init];
            model.comment = dic1[@"comment"];
            NSNumber *num = dic1[@"id"];
            model.numberid = num.stringValue;
            num = dic1[@"height"];
            model.height = num.intValue;
            num = dic1[@"width"];
            model.width = num.intValue;
            model.imgViewurl = dic1[@"imgUrl"];
            [_dataArray addObject:model];
        }
        
        [self createZoompic];
        [_mytableView reloadData];
    } AndFailure:^(NSError *error) {
        NSLog(@"error == %@",error);
        
    }];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;//根据实际选择加不加上NavigationBarHight（44、64 或者没有导航条）
    
    CGRect frame = _zoomImageView.frame;
    frame.origin.y = y;
    frame.size.height =  -y;//contentMode = UIViewContentModeScaleAspectFill时，高度改变宽度也跟着改变
    _zoomImageView.frame = frame;
    
}


#pragma mark -UITableViewDataSource,UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MainviewDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainviewDetailCell"];
    DesighModel *model = _dataArray[indexPath.row];
    //cell图片的设置
    cell.imgview.image = nil;
    [cell.imgview setImageWithURL:[NSURL URLWithString:model.imgViewurl]];
    CGFloat height1 = (viewwidth-20)*((CGFloat)model.height/model.width);
    cell.imgview.frame = CGRectMake(10, 0, viewwidth-20, height1);
    cell.imgview.tag = 100 +indexPath.row;
    cell.imgview.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
    [cell.imgview addGestureRecognizer:tap];
    
    //cell上面label的设置
    cell.label.text = model.comment;
    CGFloat height2 = [Helper heightOfString:model.comment font:[UIFont systemFontOfSize:15] width:viewwidth-20];
    cell.label.frame = CGRectMake(10, height1, viewwidth-20, height2);
    
    return cell;
}

-(void)click:(UITapGestureRecognizer *)tap
{
    [self performSegueWithIdentifier:@"detail3" sender:tap];
    
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"detail3"]) {
        UITapGestureRecognizer *tap = sender;
        SecondScrollDetailViewController *view = segue.destinationViewController;
        DesighModel *model = _dataArray[tap.view.tag-100];
        view.numberid = model.numberid;
        view.index = (int)tap.view.tag -99;
        view.array = _dataArray;
    
        
        
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DesighModel *model = _dataArray[indexPath.row];
    CGFloat height1 = (viewwidth-20)*((CGFloat)model.height/model.width);
    CGFloat height2 = [Helper heightOfString:model.comment font:[UIFont systemFontOfSize:15] width:viewwidth-20];

    return height1+height2;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//    CGFloat labelheight = [self CalculateHeithWithcontent:_comment];
    CGFloat labelheight = [self heightOfString:_comment font:[UIFont systemFontOfSize:16] width:viewwidth-20];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewwidth, 90+labelheight)];
    view.backgroundColor = [UIColor whiteColor];
    view.clipsToBounds = NO;
    //圆形白色背景
    UIImageView *whiteImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, -15, 60, 60)];
    whiteImg.backgroundColor = [UIColor whiteColor];
    whiteImg.layer.cornerRadius = 30;
    [view addSubview:whiteImg];
    [view bringSubviewToFront:whiteImg];
    
    //作者头像
    UIImageView *faceimg = [[UIImageView alloc]initWithFrame:CGRectMake(12, -13, 56, 56)];
    [faceimg setImageWithURL:[NSURL URLWithString:_facepic]];
    faceimg.layer.cornerRadius = 27;
    faceimg.clipsToBounds = YES;
    [view addSubview:faceimg];
    [view bringSubviewToFront:faceimg];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(75, 10, 150, 20)];
    label.text = [NSString stringWithFormat:@"设计师:  %@",_nick];
    label.font = [UIFont systemFontOfSize:14];
    [view addSubview:label];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(75, 30, 150, 20)];
    label1.text = [NSString stringWithFormat:@"设计费用:  %@",_feerand];
    label1.textColor = [UIColor lightGrayColor];
    label1.font = [UIFont systemFontOfSize:13];
    [view addSubview:label1];
    
    UIView *rightview = [[UIView alloc]initWithFrame:CGRectMake(view.frame.size.width-80, 10, 1, 35)];
    rightview.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view addSubview:rightview];
    
    UIView *downview = [[UIView alloc]initWithFrame:CGRectMake(0, 60, view.frame.size.width, 1)];
    downview.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view addSubview:downview];
    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 75, view.frame.size.width-20, labelheight)];
    label2.numberOfLines = 0;
    label2.lineBreakMode = NSLineBreakByWordWrapping;
    label2.font = [UIFont systemFontOfSize:16];
    label2.textAlignment = NSTextAlignmentLeft;
    label2.text = _comment;
    label2.textColor = [UIColor darkGrayColor];
    
    [view addSubview:label2];
    
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = [self heightOfString:_comment font:[UIFont systemFontOfSize:16] width:viewwidth-20];
    return 90+height;
}



-(CGFloat)heightOfString:(NSString *)string font:(UIFont *)font width:(CGFloat)width
{
    CGRect bounds;
    NSDictionary * parameterDict=[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    bounds=[string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:parameterDict context:nil];
    return bounds.size.height;
}
@end
